package com.hk;

public class RmyOptimumLocation_20210129 {

    public static void main(String[] args) {

        String a = "辽宁省;韩国;辽宁";
        String s = locationExactMatch(a);
        System.out.println("s = " + s);

    }

    public static String locationExactMatch(String text) {
        int start = 0;
        String[] allPath = text.split(";");
        String originalLocation = null;
        if (allPath.length >= 2) {

            String sub = allPath[0];
            originalLocation = sub;
            System.out.println(text.indexOf(sub, start));
            while ((start = text.indexOf(sub, start)) >= 0) {
                start += sub.length();
                System.out.println("start = " + start);

                System.out.println("text = " + text.length() + "         "  +  (start + 1) );
                if ((start + 1) <=  text.length()){
                    String substring = text.substring(start, start + 1);
                System.out.println("text = " + text.length());
                    System.out.println("substring = " + substring);
                    if (!substring.equals(";")) {

                        String matchingLocation = text.substring(start - sub.length(), text.length()).split(";")[0];
                        if (matchingLocation.length() > originalLocation.length()) {
                            originalLocation = matchingLocation;
                        }
                    }
                }
                }
            }
        return originalLocation;
    }
}
